package com.rwx.temp;

import lombok.extern.slf4j.Slf4j;

import static com.rwx.n2.util.Sleeper.sleep;

@Slf4j(topic = "c.ThreadTwoProtect")
public class ThreadTwoProtect {

    public static void main(String[] args) {
        TwoProThread t = new TwoProThread();
        t.start();
        sleep(6);
        log.debug("开始执行打断");

        t.stop();

    }


}
@Slf4j(topic = "c.TwoProThread")
class TwoProThread{
    Thread t=null;
    public void start() {
        t=new Thread(){
            @Override
            public void run() {
                while (true){
                    Thread thread = Thread.currentThread();
                    boolean interrupted = thread.isInterrupted();
                    if(interrupted){
                        log.debug("被打断，料理后事");
                        break;
                    }
                    try {
                        sleep(2000);

                        log.debug("执行监控记录");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        //打断休眠后 打断状态会被重置
                        //再执行一次，当进入while时则可以执行break
                        thread.interrupt();

                    }

                }
            }
        };
        t.start();
    }


    public void stop(){
        t.interrupt();
    }


}


